***MIPAC DOFILE***

***Dataset
clear
use mipac_dta

**Rescale and change polarity
replace gcb_bribery_courts = gcb_bribery_courts*100
replace gcb_bribery_education = gcb_bribery_education*100
replace gcb_bribery_health = gcb_bribery_health*100
replace gcb_bribery_documents = gcb_bribery_documents*100
replace gcb_bribery_police = gcb_bribery_police*100
replace gcb_bribery_tax_officials = gcb_bribery_tax_officials*100
replace gcb_bribery_utilities = gcb_bribery_utilities*100
replace gcb_bribery_tax_officials = gcb_bribery_tax_officials*100
replace gcb_sextortion = gcb_sextortion*100
replace gcb_vote_buying = gcb_vote_buying*100
replace gcb_bankers = gcb_bankers*100
replace gcb_business_executives = gcb_business_executives*100
replace gcb_foreign_companies = gcb_foreign_companies*100
replace gcb_journalist_media = gcb_journalist_media*100
replace gcb_legislative = gcb_legislative*100
replace gcb_military = gcb_military*100
replace gcb_bribery_land_services = gcb_bribery_land_services*100

replace gcb_bribery_tax_officials=gcb_bribery_tax_officials/100

replace afrob_bribexdocument = afrob_bribexdocument*100
replace afrob_bribexwater_services = afrob_bribexwater_services*100
replace afrob_bribexhealth_services = afrob_bribexhealth_services*100
replace afrob_bribexpolice = afrob_bribexpolice*100
replace afrob_bribexschool = afrob_bribexschool*100
replace afrob_vote_buying = afrob_vote_buying*100
replace afrob_bribexcourts = afrob_bribexcourts*100
replace afrob_legislative = afrob_legislative*100
replace afrob_business_executives = afrob_business_executives*100

replace latinob_bribexdocument = latinob_bribexdocument*100
replace latinob_bribexwater_services = latinob_bribexwater_services*100
replace latinob_bribexhealth_services = latinob_bribexhealth_services*100
replace latinob_bribexpolice = latinob_bribexpolice*100
replace latinob_bribexschool = latinob_bribexschool*100
replace latinob_bribexcourts = latinob_bribexcourts*100
replace latinob_legislative = latinob_legislative*100
replace latinob_business_executives = latinob_business_executives*100

replace wjp_legislative = 100-( wjp_legislative*100)

replace wef_diversion_funds = 100-((wef_diversion_funds-1)/6*100)
replace wef_irregular_payments = 100-((wef_irregular_payments-1)/6*100)
replace wef_favoritism = 100-((wef_favoritism-1)/6*100)

replace vdem_judicial = 100-( vdem_judicial /4*100)
replace vdem_legislative = 100-( vdem_legislative /4*100)
replace vdem_bribe_exe = 100-(vdem_bribe_exe /4*100)
replace vdem_embezz_exe = 100-( vdem_embezz_exe /4*100)
replace vdem_public_sector_bribe = 100-(vdem_public_sector_bribe/4*100)
replace vdem_public_sector_theft = 100-(vdem_public_sector_theft/4*100)
replace vdem_vote_buying = 100-(vdem_vote_buying/4*100)


** Generate Dimensions
gen dim1 = vdem_bribe_exe
egen dim2 = rowmean( gcb_vote_buying vdem_vote_buying wvs_vote_buying afrob_vote_buying)
gen dim3 = vdem_embezz_exe
egen dim4 = rowmean( gcb_legislative vdem_legislative wjp_legislative afrob_legislative latinob_legislative )
egen dim5 = rowmean( vdem_judicial gcb_bribery_courts afrob_bribexcourts eurob_bribexcourts latinob_bribexcourts )
gen dim6 = gcb_military
egen dim7 = rowmean( gcb_bribery_police afrob_bribexpolice eurob_bribexpolice latinob_bribexpolice)
egen dim8 = rowmean( gcb_bribery_health afrob_bribexhealth_services eurob_bribexhealthcare_system latinob_bribexhealth_services )
egen dim9 = rowmean( gcb_bribery_education eurob_bribexeducation_sector afrob_bribexschool latinob_bribexschool )
egen dim10 = rowmean( gcb_bribery_utilities afrob_bribexwater_services latinob_bribexwater_services)
egen dim11 = rowmean (gcb_bribery_land_services eurob_bribexbuilding_permits)
egen dim12 = rowmean( gcb_bribery_documents afrob_bribexdocument latinob_bribexdocument eurob_bribexinspectors eurob_bribexbusiness_permits)
egen dim13 = rowmean(gcb_bribery_tax_officials eurob_bribextax_officials)
gen dim14 = gcb_sextortion
gen dim15 = vdem_public_sector_theft
egen dim16 = rowmean(gcb_business_executives wvs_business_executives afrob_business_executives latinob_business_executives eurob_private_companies)
egen dim17 = rowmean( gcb_bankers eurob_banks_financial)
egen dim18 = rowmean(wvs_journalists_media gcb_journalist_media)
egen dim19 = rowmean( wef_irregular_payments unodc_bribe_firms eurob_bribe_firms)
egen dim20 = rowmean( wef_favoritism eurob_patronage_nepotism)
gen dim21 = wef_diversion_funds

drop if year<2013

bysort year: egen sd1=sd(dim1)
bysort year: egen sd2=sd(dim2)
bysort year: egen sd3=sd(dim3)
bysort year: egen sd4=sd(dim4)
bysort year: egen sd5=sd(dim5)
bysort year: egen sd6=sd(dim6)
bysort year: egen sd7=sd(dim7)
bysort year: egen sd8=sd(dim8)
bysort year: egen sd9=sd(dim9)
bysort year: egen sd10=sd(dim10)
bysort year: egen sd11=sd(dim11)
bysort year: egen sd12=sd(dim12)
bysort year: egen sd13=sd(dim13)
bysort year: egen sd14=sd(dim14)
bysort year: egen sd15=sd(dim15)
bysort year: egen sd16=sd(dim16)
bysort year: egen sd17=sd(dim17)
bysort year: egen sd18=sd(dim18)
bysort year: egen sd19=sd(dim19)
bysort year: egen sd20=sd(dim20)
bysort year: egen sd21=sd(dim21)


save mipac_dims.dta, replace


*#Dimension 1st
clear
use mipac_dims
keep country year dim1 sd1
reshape wide dim1 sd1, i(country) j(year)
gen cdim1 = dim12022-dim12015
gen pcdim1 = (dim12022-dim12015)/dim12015*100
gen threshold1 = sd12015*dim12015/100

gen test = 1 if (cdim1==. & dim12013==. & dim12014==. & dim12015==. & dim12016==. & dim12017==. & dim12018==. & dim12019==. & dim12020==. & dim12021==. & dim12022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim1==. & test == .)

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I1 = 1 if cdim1<0
replace I1 = 0 if cdim1>=0
replace I1=. if cdim1==.

gen dim1_th = 1 if (I1==1) & ((cdim1*(-1))>=threshold) & (test != 1)
replace dim1_th = 0 if (dim1_th==.) & (test != 1)
replace dim1_th = . if cdim1==.

gen dim1_50 = 1 if (I1==1) & ((pcdim1*(-1))>=50) & (test != 1)
replace dim1_50 = 0 if (dim1_50==.) & (test != 1)
replace dim1_50 = . if pcdim1==.

gen dim1_25 = 1 if (I1==1) & ((pcdim1*(-1))>=25) & (test != 1)
replace dim1_25 = 0 if (dim1_25==.) & (test != 1)
replace dim1_25 = . if pcdim1==.

gen dim1_10 = 1 if (I1==1) & ((pcdim1*(-1))>=10) & (test != 1)
replace dim1_10 = 0 if (dim1_10==.) & (test != 1)
replace dim1_10 = . if pcdim1==.

replace thres=. if cdim==.
browse country cd pc I thres dim1_*
tab I
tab dim1_th
tab dim1_50
tab dim1_25
tab dim1_10

keep country cd pc I thres dim1_*
save dim1.dta, replace



*#Dimension 2nd
clear
use mipac_dims
keep country year dim2 sd2
reshape wide dim2 sd2, i(country) j(year)
gen cdim2 = dim22022-dim22015
gen pcdim2 = (dim22022-dim22015)/dim22015*100
gen threshold2 = sd22015*dim22015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim2==. & dim22013==. & dim22014==. & dim22015==. & dim22016==. & dim22017==. & dim22018==. & dim22019==. & dim22020==. & dim22021==. & dim22022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim2==. & test == .)
*Special treatments
browse country dim* if test2==1

tab country if test2==1
replace cdim2 = dim22021-dim22015 if country=="Afghanistan"
replace pcdim2 = (dim22021-dim22015)/dim22015*100 if country=="Afghanistan"

replace cdim2 = dim22021-dim22015 if country=="Chad"
replace pcdim2 = (dim22021-dim22015)/dim22015*100 if country=="Chad"

replace cdim2 = dim22021-dim22015 if country=="Guinea"
replace pcdim2 = (dim22021-dim22015)/dim22015*100 if country=="Guinea"

replace cdim2 = dim22021-dim22015 if country=="Haiti"
replace pcdim2 = (dim22021-dim22015)/dim22015*100 if country=="Haiti"

replace cdim2 = dim22020-dim22015 if country=="Mali"
replace pcdim2 = (dim22020-dim22015)/dim22015*100 if country=="Mali"

replace cdim2 = dim22021-dim22015 if country=="Myanmar"
replace pcdim2 = (dim22021-dim22015)/dim22015*100 if country=="Myanmar"

replace cdim2 = dim22019-dim22015 if country=="Sudan"
replace pcdim2 = (dim22019-dim22015)/dim22015*100 if country=="Sudan"

replace cdim2 = dim22022-dim22021 if country=="Qatar"
replace pcdim2 = (dim22022-dim22021)/dim22021*100 if country=="Qatar"

replace cdim2 = dim22019-dim22013 if country=="Palestine"
replace pcdim2 = (dim22019-dim22013)/dim22013*100 if country=="Palestine"

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I2 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim2_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim2_th = 0 if (dim2_th==.) & (test != 1)
replace dim2_th = . if cdim==.

gen dim2_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim2_50 = 0 if (dim2_50==.) & (test != 1)
replace dim2_50 = . if pcdim==.

gen dim2_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim2_25 = 0 if (dim2_25==.) & (test != 1)
replace dim2_25 = . if pcdim==.

gen dim2_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim2_10 = 0 if (dim2_10==.) & (test != 1)
replace dim2_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim2_*
tab I
tab dim2_th
tab dim2_50
tab dim2_25
tab dim2_10

keep country cd pc I thres dim2_*
save dim2.dta, replace


*#Dimension 3rd
clear
use mipac_dims
keep country year dim3 sd3
reshape wide dim3 sd3, i(country) j(year)
gen cdim3 = dim32022-dim32015
gen pcdim3 = (dim32022-dim32015)/dim32015*100
gen threshold3 = sd32015*dim32015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim3==. & dim32013==. & dim32014==. & dim32015==. & dim32016==. & dim32017==. & dim32018==. & dim32019==. & dim32020==. & dim32021==. & dim32022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim3==. & test == .)

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I3 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim3_th = 1 if (I==1) & ((cdim3*(-1))>=threshold) & (test != 1)
replace dim3_th = 0 if (dim3_th==.) & (test != 1 )
replace dim3_th = . if cdim3==.

gen dim3_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim3_50 = 0 if (dim3_50==.) & (test != 1)
replace dim3_50 = . if pcdim==.

gen dim3_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim3_25 = 0 if (dim3_25==.) & (test != 1)
replace dim3_25 = . if pcdim==.

gen dim3_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim3_10 = 0 if (dim3_10==.) & (test != 1)
replace dim3_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim3_*
tab I
tab dim3_th
tab dim3_50
tab dim3_25
tab dim3_10

keep country cd pc I thres dim3_*
save dim3.dta, replace



*#Dimension 4th
clear
use mipac_dims
keep country year dim4 sd4
reshape wide dim4 sd4, i(country) j(year)
gen cdim4 = dim42022-dim42015
gen pcdim4 = (dim42022-dim42015)/dim42015*100
gen threshold4 = sd42015*dim42015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim4==. & dim42013==. & dim42014==. & dim42015==. & dim42016==. & dim42017==. & dim42018==. & dim42019==. & dim42020==. & dim42021==. & dim42022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim4==. & test == .)
*Special treatments
tab country if test2==1
browse country dim* cdim if test2==1

replace cdim4 = dim42021-dim42015 if test2==1  & cdim4==. & dim42022==. & dim42021!=.
replace pcdim4 = (dim42021-dim42015)/dim42015*100 if test2==1  & pcdim4==. & dim42022==. & dim42021!=.

replace threshold4 = sd42016*dim42016/100 if test2==1 & cdim4==. & dim42015==. & dim42016!=.  
replace cdim4 = dim42022-dim42016 if test2==1 & dim42022!=.  & cdim4==. & dim42015==. & dim42016!=.
replace pcdim4 = (dim42022-dim42016)/dim42016*100 if test2==1 & dim42022!=.  & pcdim4==. & dim42015==. & dim42016!=.

replace threshold4 = sd42013*dim42013/100 if country=="Palestine"
replace cdim4 = dim42019-dim42013 if country=="Palestine"
replace pcdim4 = (dim42019-dim42013)/dim42013*100 if country=="Palestine"

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I4 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim4_th = 1 if (I==1) & ((cdim4*(-1))>=threshold) & (test != 1)
replace dim4_th = 0 if (dim4_th==.) & (test != 1 )
replace dim4_th = . if cdim4==.

gen dim4_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim4_50 = 0 if (dim4_50==.) & (test != 1)
replace dim4_50 = . if pcdim==.

gen dim4_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim4_25 = 0 if (dim4_25==.) & (test != 1)
replace dim4_25 = . if pcdim==.

gen dim4_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim4_10 = 0 if (dim4_10==.) & (test != 1)
replace dim4_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim4_*
tab I
tab dim4_th
tab dim4_50
tab dim4_25
tab dim4_10

keep country cd pc I thres dim4_*
save dim4.dta, replace


*#Dimension 5th
clear
use mipac_dims
keep country year dim5 sd5
reshape wide dim5 sd5, i(country) j(year)
gen cdim5 = dim52022-dim52015
gen pcdim5 = (dim52022-dim52015)/dim52015*100
gen threshold5 = sd52015*dim52015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim5==. & dim52013==. & dim52014==. & dim52015==. & dim52016==. & dim52017==. & dim52018==. & dim52019==. & dim52020==. & dim52021==. & dim52022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim5==. & test == .)
*Special treatments
browse country dim* cdim if test2==1

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I5 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim5_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim5_th = 0 if (dim5_th==.) & (test != 1 )
replace dim5_th = . if cdim==.

gen dim5_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim5_50 = 0 if (dim5_50==.) & (test != 1)
replace dim5_50 = . if pcdim==.

gen dim5_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim5_25 = 0 if (dim5_25==.) & (test != 1)
replace dim5_25 = . if pcdim==.

gen dim5_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim5_10 = 0 if (dim5_10==.) & (test != 1)
replace dim5_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim5_*
tab I
tab dim5_th
tab dim5_50
tab dim5_25
tab dim5_10

keep country cd pc I thres dim5_*
save dim5.dta, replace



*#Dimension 6th
clear
use mipac_dims
keep country year dim6 sd6
reshape wide dim6 sd6, i(country) j(year)
gen cdim6 = dim62021-dim62013
gen pcdim6 = (dim62021-dim62013)/dim62013*100
gen threshold6 = sd62013*dim62013/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim6==. & dim62013==. & dim62014==. & dim62015==. & dim62016==. & dim62017==. & dim62018==. & dim62019==. & dim62020==. & dim62021==. & dim62022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim6==. & test == .)
*Special treatments
browse country dim* cdim if test2==1
tab country if test2==1

replace cdim6 = dim62020-dim62013 if test2==1 & dim62022==. & dim62021==.  & dim62020!=. & cdim6==.
replace pcdim6 = (dim62020-dim62013)/dim62013*100 if test2==1 & dim62022==. & dim62021==.  & dim62020!=. & pcdim6==.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I6 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim6_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim6_th = 0 if (dim6_th==.) & (test != 1 )
replace dim6_th = . if cdim==.

gen dim6_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim6_50 = 0 if (dim6_50==.) & (test != 1)
replace dim6_50 = . if pcdim==.

gen dim6_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim6_25 = 0 if (dim6_25==.) & (test != 1)
replace dim6_25 = . if pcdim==.

gen dim6_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim6_10 = 0 if (dim6_10==.) & (test != 1)
replace dim6_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim6_*
tab I
tab dim6_th
tab dim6_50
tab dim6_25
tab dim6_10

keep country cd pc I thres dim6_*
save dim6.dta, replace


*#Dimension 7th
clear
use mipac_dims
keep country year dim7 sd7
reshape wide dim7 sd7, i(country) j(year)
gen cdim7 = dim72022-dim72015
gen pcdim7 = (dim72022-dim72015)/dim72015*100
gen threshold7 = sd72015*dim72015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim7==. & dim72013==. & dim72014==. & dim72015==. & dim72016==. & dim72017==. & dim72018==. & dim72019==. & dim72020==. & dim72021==. & dim72022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim7==. & test == .)
*Special treatments
browse country cdim7 if test2==1
tab country if test2==1

replace cdim7 = dim72021-dim72015 if test2==1 & cdim7==. & dim72022==. & dim72021!=. & dim72015!=.
replace pcdim7 = (dim72021-dim72015)/dim72015*100 if test2==1 & pcdim7==. & dim72022==. & dim72021!=. & dim72015!=.
replace cdim7 = dim72020-dim72015 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015!=.
replace pcdim7 = (dim72020-dim72015)/dim72015*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015!=.
replace cdim7 = dim72019-dim72015 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015!=.
replace pcdim7 = (dim72019-dim72015)/dim72015*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015!=.

replace threshold7 = sd72013*dim72013/100 if test2==1 & cdim7==. & dim72015==. & dim72013!=. 
replace cdim7 = dim72022-dim72013 if test2==1 & cdim7==. & dim72022!=. & dim72015==. & dim72013!=.
replace pcdim7 = (dim72022-dim72013)/dim72013*100 if test2==1 & pcdim7==. & dim72022!=. & dim72015==. & dim72013!=.
replace cdim7 = dim72021-dim72013 if test2==1 & cdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013!=.
replace pcdim7 = (dim72021-dim72013)/dim72013*100 if test2==1 & pcdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013!=.
replace cdim7 = dim72020-dim72013 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015==. & dim72013!=.
replace pcdim7 = (dim72020-dim72013)/dim72013*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015==. & dim72013!=.
replace cdim7 = dim72019-dim72013 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013!=.
replace pcdim7 = (dim72019-dim72013)/dim72013*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013!=.

replace threshold7 = sd72016*dim72016/100 if test2==1 & cdim7==. & dim72015==. & dim72013==. & dim72016!=.
replace cdim7 = dim72019-dim72016 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013==. & dim72016!=.
replace pcdim7 = (dim72019-dim72016)/dim72016*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013==. & dim72016!=.

replace threshold7 = sd72017*dim72017/100 if test2==1 & cdim7==. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace cdim7 = dim72021-dim72017 if test2==1 & cdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace pcdim7 = (dim72021-dim72017)/dim72017*100 if test2==1 & pcdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace cdim7 = dim72020-dim72017 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace pcdim7 = (dim72020-dim72017)/dim72017*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace cdim7 = dim72019-dim72017 if test2==1 & cdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.
replace pcdim7 = (dim72019-dim72017)/dim72017*100 if test2==1 & pcdim7==. & dim72022==. & dim72021==. & dim72020==. & dim72019!=. & dim72015==. & dim72013==. & dim72016==. & dim72017!=.

replace threshold7 = sd72019*dim72019/100 if test2==1 & cdim7==. & dim72015==. & dim72013==. & dim72016==. & dim72017==. & dim72019!=.
replace cdim7 = dim72021-dim72019 if test2==1 & cdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013==. & dim72016==. & dim72017==. & dim72019!=.
replace pcdim7 = (dim72021-dim72019)/dim72019*100 if test2==1 & pcdim7==. & dim72022==. & dim72021!=. & dim72015==. & dim72013==. & dim72016==. & dim72017==. & dim72019!=.


replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.	
gen I7 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim7_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim7_th = 0 if (dim7_th==.) & (test != 1 )
replace dim7_th = . if cdim==.

gen dim7_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim7_50 = 0 if (dim7_50==.) & (test != 1)
replace dim7_50 = . if pcdim==.

gen dim7_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim7_25 = 0 if (dim7_25==.) & (test != 1)
replace dim7_25 = . if pcdim==.

gen dim7_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim7_10 = 0 if (dim7_10==.) & (test != 1)
replace dim7_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim7_*
tab I
tab dim7_th
tab dim7_50
tab dim7_25
tab dim7_10

keep country cd pc I thres dim7_*
save dim7.dta, replace



*#Dimension 8th
clear
use mipac_dims
keep country year dim8 sd8
reshape wide dim8 sd8, i(country) j(year)
gen cdim8 = dim82022-dim82015
gen pcdim8 = (dim82022-dim82015)/dim82015*100
gen threshold8 = sd82015*dim82015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim8==. & dim82013==. & dim82014==. & dim82015==. & dim82016==. & dim82017==. & dim82018==. & dim82019==. & dim82020==. & dim82021==. & dim82022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim8==. & test == .)
*Special treatments
browse country dim* cdim8 pcdim8 if test2==1

replace cdim8 = dim82021-dim82015 if test2==1 & cdim8==. & dim82022==. & dim82021!=. & dim82015!=.
replace pcdim8 = (dim82021-dim82015)/dim82015*100 if test2==1 & pcdim8==. & dim82022==. & dim82021!=. & dim82015!=.
replace cdim8 = dim82020-dim82015 if test2==1 & cdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015!=.
replace pcdim8 = (dim82020-dim82015)/dim82015*100 if test2==1 & pcdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015!=.

replace threshold8 = sd82013*dim82013/100 if test2==1 & cdim8==. & dim82015==. & dim82013!=. 
replace cdim8 = dim82022-dim82013 if test2==1 & cdim8==. & dim82022!=. & dim82015==. & dim82013!=.
replace pcdim8 = (dim82022-dim82013)/dim82013*100 if test2==1 & pcdim8==. & dim82022!=. & dim82015==. & dim82013!=.
replace cdim8 = dim82021-dim82013 if test2==1 & cdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013!=.
replace pcdim8 = (dim82021-dim82013)/dim82013*100 if test2==1 & pcdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013!=.
replace cdim8 = dim82020-dim82013 if test2==1 & cdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015==. & dim82013!=.
replace pcdim8 = (dim82020-dim82013)/dim82013*100 if test2==1 & pcdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015==. & dim82013!=.
replace cdim8 = dim82019-dim82013 if test2==1 & cdim8==. & dim82022==. & dim82021==. & dim82020==. & dim82019!=. & dim82015==. & dim82013!=.
replace pcdim8 = (dim82019-dim82013)/dim82013*100 if test2==1 & pcdim8==. & dim82022==. & dim82021==. & dim82020==. & dim82019!=. & dim82015==. & dim82013!=.

replace threshold8 = sd82016*dim82016/100 if test2==1 & cdim8==. & dim82015==. & dim82013==. & dim82016!=.
replace cdim8 = dim82019-dim82016 if test2==1 & cdim8==. & dim82022==. & dim82021==. & dim82020==. & dim82019!=. & dim82015==. & dim82013==. & dim82016!=.
replace pcdim8 = (dim82019-dim82016)/dim82016*100 if test2==1 & pcdim8==. & dim82022==. & dim82021==. & dim82020==. & dim82019!=. & dim82015==. & dim82013==. & dim82016!=.

replace threshold8 = sd82017*dim82017/100 if test2==1 & cdim8==. & dim82015==. & dim82013==. & dim82016==. & dim82017!=.
replace cdim8 = dim82021-dim82017 if test2==1 & cdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013==. & dim82016==. & dim82017!=.
replace pcdim8 = (dim82021-dim82017)/dim82017*100 if test2==1 & pcdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013==. & dim82016==. & dim82017!=.
replace cdim8 = dim82020-dim82017 if test2==1 & cdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015==. & dim82013==. & dim82016==. & dim82017!=.
replace pcdim8 = (dim82020-dim82017)/dim82017*100 if test2==1 & pcdim8==. & dim82022==. & dim82021==. & dim82020!=. & dim82015==. & dim82013==. & dim82016==. & dim82017!=.

replace threshold8 = sd82019*dim82019/100 if test2==1 & cdim8==. & dim82015==. & dim82013==. & dim82016==. & dim82017==. & dim82019!=.
replace cdim8 = dim82021-dim82019 if test2==1 & cdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013==. & dim82016==. & dim82017==. & dim82019!=.
replace pcdim8 = (dim82021-dim82019)/dim82019*100 if test2==1 & pcdim8==. & dim82022==. & dim82021!=. & dim82015==. & dim82013==. & dim82016==. & dim82017==. & dim82019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I8 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim8_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim8_th = 0 if (dim8_th==.) & (test != 1 )
replace dim8_th = . if cdim==.

gen dim8_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim8_50 = 0 if (dim8_50==.) & (test != 1)
replace dim8_50 = . if pcdim==.

gen dim8_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim8_25 = 0 if (dim8_25==.) & (test != 1)
replace dim8_25 = . if pcdim==.

gen dim8_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim8_10 = 0 if (dim8_10==.) & (test != 1)
replace dim8_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim8_*
tab I
tab dim8_th
tab dim8_50
tab dim8_25
tab dim8_10

keep country cd pc I thres dim8_*
save dim8.dta, replace


*#Dimension 9th
clear
use mipac_dims
keep country year dim9 sd9
reshape wide dim9 sd9, i(country) j(year)
gen cdim9 = dim92022-dim92015
gen pcdim9 = (dim92022-dim92015)/dim92015*100
gen threshold9 = sd92015*dim92015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim9==. & dim92013==. & dim92014==. & dim92015==. & dim92016==. & dim92017==. & dim92018==. & dim92019==. & dim92020==. & dim92021==. & dim92022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim9==. & test == .)
*Special treatments
browse country dim* cdim9 if test2==1

replace cdim9 = dim92021-dim92015 if test2==1 & cdim9==. & dim92022==. & dim92021!=. & dim92015!=.
replace pcdim9 = (dim92021-dim92015)/dim92015*100 if test2==1 & pcdim9==. & dim92022==. & dim92021!=. & dim92015!=.
replace cdim9 = dim92020-dim92015 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015!=.
replace pcdim9 = (dim92020-dim92015)/dim92015*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015!=.
replace cdim9 = dim92019-dim92015 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015!=.
replace pcdim9 = (dim92019-dim92015)/dim92015*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015!=.

replace threshold9 = sd92013*dim92013/100 if test2==1 & cdim9==. & dim92015==. & dim92013!=. 
replace cdim9 = dim92022-dim92013 if test2==1 & cdim9==. & dim92022!=. & dim92015==. & dim92013!=.
replace pcdim9 = (dim92022-dim92013)/dim92013*100 if test2==1 & pcdim9==. & dim92022!=. & dim92015==. & dim92013!=.
replace cdim9 = dim92021-dim92013 if test2==1 & cdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013!=.
replace pcdim9 = (dim92021-dim92013)/dim92013*100 if test2==1 & pcdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013!=.
replace cdim9 = dim92020-dim92013 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015==. & dim92013!=.
replace pcdim9 = (dim92020-dim92013)/dim92013*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015==. & dim92013!=.
replace cdim9 = dim92019-dim92013 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015==. & dim92013!=.
replace pcdim9 = (dim92019-dim92013)/dim92013*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015==. & dim92013!=.

replace threshold9 = sd92016*dim92016/100 if test2==1 & cdim9==. & dim92015==. & dim92013==. & dim92016!=.
replace cdim9 = dim92019-dim92016 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015==. & dim92013==. & dim92016!=.
replace pcdim9 = (dim92019-dim92016)/dim92016*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020==. & dim92019!=. & dim92015==. & dim92013==. & dim92016!=.

replace threshold9 = sd92017*dim92017/100 if test2==1 & cdim9==. & dim92015==. & dim92013==. & dim92016==. & dim92017!=.
replace cdim9 = dim92021-dim92017 if test2==1 & cdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013==. & dim92016==. & dim92017!=.
replace pcdim9 = (dim92021-dim92017)/dim92017*100 if test2==1 & pcdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013==. & dim92016==. & dim92017!=.
replace cdim9 = dim92020-dim92017 if test2==1 & cdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015==. & dim92013==. & dim92016==. & dim92017!=.
replace pcdim9 = (dim92020-dim92017)/dim92017*100 if test2==1 & pcdim9==. & dim92022==. & dim92021==. & dim92020!=. & dim92015==. & dim92013==. & dim92016==. & dim92017!=.

replace threshold9 = sd92019*dim92019/100 if test2==1 & cdim9==. & dim92015==. & dim92013==. & dim92016==. & dim92017==. & dim92019!=.
replace cdim9 = dim92021-dim92019 if test2==1 & cdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013==. & dim92016==. & dim92017==. & dim92019!=.
replace pcdim9 = (dim92021-dim92019)/dim92019*100 if test2==1 & pcdim9==. & dim92022==. & dim92021!=. & dim92015==. & dim92013==. & dim92016==. & dim92017==. & dim92019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I9 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim9_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim9_th = 0 if (dim9_th==.) & (test != 1 )
replace dim9_th = . if cdim==.

gen dim9_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim9_50 = 0 if (dim9_50==.) & (test != 1)
replace dim9_50 = . if pcdim==.

gen dim9_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim9_25 = 0 if (dim9_25==.) & (test != 1)
replace dim9_25 = . if pcdim==.

gen dim9_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim9_10 = 0 if (dim9_10==.) & (test != 1)
replace dim9_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim9_*
tab I
tab dim9_th
tab dim9_50
tab dim9_25
tab dim9_10

keep country cd pc I thres dim9_*
save dim9.dta, replace



*#Dimension 10th
clear
use mipac_dims
keep country year dim10 sd10
reshape wide dim10 sd10, i(country) j(year)
gen cdim10 = dim102021-dim102015
gen pcdim10 = (dim102021-dim102015)/dim102015*100
gen threshold10 = sd102015*dim102015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim10==. & dim102013==. & dim102014==. & dim102015==. & dim102016==. & dim102017==. & dim102018==. & dim102019==. & dim102020==. & dim102021==. & dim102022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim10==. & test == .)
*Special treatments
browse country dim* cdim10 if test2==1

replace cdim10 = dim102020-dim102015 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015!=.
replace pcdim10 = (dim102020-dim102015)/dim102015*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015!=.
replace cdim10 = dim102019-dim102015 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015!=.
replace pcdim10 = (dim102019-dim102015)/dim102015*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015!=.

replace threshold10 = sd102013*dim102013/100 if test2==1 & cdim10==. & dim102015==. & dim102013!=. 
replace cdim10 = dim102021-dim102013 if test2==1 & cdim10==. & dim102022==. & dim102021!=. & dim102015==. & dim102013!=.
replace pcdim10 = (dim102021-dim102013)/dim102013*100 if test2==1 & pcdim10==. & dim102022==. & dim102021!=. & dim102015==. & dim102013!=.
replace cdim10 = dim102020-dim102013 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013!=.
replace pcdim10 = (dim102020-dim102013)/dim102013*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013!=.
replace cdim10 = dim102019-dim102013 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013!=.
replace pcdim10 = (dim102019-dim102013)/dim102013*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013!=.

replace threshold10 = sd102016*dim102016/100 if test2==1 & cdim10==. & dim102015==. & dim102013==. & dim102016!=.
replace cdim10 = dim102019-dim102016 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013==. & dim102016!=.
replace pcdim10 = (dim102019-dim102016)/dim102016*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013==. & dim102016!=.

replace threshold10 = sd102017*dim102017/100 if test2==1 & cdim10==. & dim102015==. & dim102013==. & dim102016==. & dim102017!=.
replace cdim10 = dim102020-dim102017 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013==. & dim102016==. & dim102017!=.
replace pcdim10 = (dim102020-dim102017)/dim102017*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013==. & dim102016==. & dim102017!=.
replace cdim10 = dim102019-dim102017 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013==. & dim102016==. & dim102017!=.
replace pcdim10 = (dim102019-dim102017)/dim102017*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020==. & dim102019!=. & dim102015==. & dim102013==. & dim102016==. & dim102017!=.

replace threshold10 = sd102019*dim102019/100 if test2==1 & cdim10==. & dim102015==. & dim102013==. & dim102016==. & dim102017==. & dim102019!=.
replace cdim10 = dim102020-dim102019 if test2==1 & cdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013==. & dim102016==. & dim102017==. & dim102019!=.
replace pcdim10 = (dim102020-dim102019)/dim102019*100 if test2==1 & pcdim10==. & dim102022==. & dim102021==. & dim102020!=. & dim102015==. & dim102013==. & dim102016==. & dim102017==. & dim102019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I10 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim10_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim10_th = 0 if (dim10_th==.) & (test != 1 )
replace dim10_th = . if cdim==.

gen dim10_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim10_50 = 0 if (dim10_50==.) & (test != 1)
replace dim10_50 = . if pcdim==.

gen dim10_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim10_25 = 0 if (dim10_25==.) & (test != 1)
replace dim10_25 = . if pcdim==.

gen dim10_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim10_10 = 0 if (dim10_10==.) & (test != 1)
replace dim10_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim10_*
tab I
tab dim10_th
tab dim10_50
tab dim10_25
tab dim10_10

keep country cd pc I thres dim10_*
save dim10.dta, replace


*#Dimension 11th
clear
use mipac_dims
keep country year dim11 sd11
reshape wide dim11 sd11, i(country) j(year)
gen cdim11 = dim112022-dim112013
gen pcdim11 = (dim112022-dim112013)/dim112013*100
gen threshold11 = sd112013*dim112013/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim11==. & dim112013==. & dim112014==. & dim112015==. & dim112016==. & dim112017==. & dim112018==. & dim112019==. & dim112020==. & dim112021==. & dim112022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim11==. & test == .)
*Special treatments
browse country dim* cdim11 if test2==1

replace cdim11 = dim112019-dim112013 if test2==1 & cdim11==. & dim112022==. & dim112021==. & dim112020==. & dim112019!=. & dim112015==. & dim112013!=.
replace pcdim11 = (dim112019-dim112013)/dim112013*100 if test2==1 & pcdim11==. & dim112022==. & dim112021==. & dim112020==. & dim112019!=. & dim112015==. & dim112013!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I11 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim11_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim11_th = 0 if (dim11_th==.) & (test != 1 )
replace dim11_th = . if cdim==.

gen dim11_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim11_50 = 0 if (dim11_50==.) & (test != 1)
replace dim11_50 = . if pcdim==.

gen dim11_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim11_25 = 0 if (dim11_25==.) & (test != 1)
replace dim11_25 = . if pcdim==.

gen dim11_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim11_10 = 0 if (dim11_10==.) & (test != 1)
replace dim11_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim11_*
tab I
tab dim11_th
tab dim11_50
tab dim11_25
tab dim11_10

keep country cd pc I thres dim11_*
save dim11.dta, replace


*#Dimension 12th
clear
use mipac_dims
keep country year dim12 sd12
reshape wide dim12 sd12, i(country) j(year)
gen cdim12 = dim122022-dim122015
gen pcdim12 = (dim122022-dim122015)/dim122015*100
gen threshold12 = sd122015*dim122015/100
*Test1 = Test if there is not info at any year
gen test = 1 if (cdim12==. & dim122013==. & dim122014==. & dim122015==. & dim122016==. & dim122017==. & dim122018==. & dim122019==. & dim122020==. & dim122021==. & dim122022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim12==. & test == .)
*Special treatments
browse country dim* cdim12 if test2==1

replace cdim12 = dim122021-dim122015 if test2==1 & cdim12==. & dim122022==. & dim122021!=. & dim122015!=.
replace pcdim12 = (dim122021-dim122015)/dim122015*100 if test2==1 & pcdim12==. & dim122022==. & dim122021!=. & dim122015!=.
replace cdim12 = dim122020-dim122015 if test2==1 & cdim12==. & dim122022==. & dim122021==. & dim122020!=. & dim122015!=.
replace pcdim12 = (dim122020-dim122015)/dim122015*100 if test2==1 & pcdim12==. & dim122022==. & dim122021==. & dim122020!=. & dim122015!=.

replace threshold12 = sd122013*dim122013/100 if test2==1 & cdim12==. & dim122015==. & dim122013!=. 
replace cdim12 = dim122022-dim122013 if test2==1 & cdim12==. & dim122022!=. & dim122015==. & dim122013!=.
replace pcdim12 = (dim122022-dim122013)/dim122013*100 if test2==1 & pcdim12==. & dim122022!=. & dim122015==. & dim122013!=.
replace cdim12 = dim122021-dim122013 if test2==1 & cdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013!=.
replace pcdim12 = (dim122021-dim122013)/dim122013*100 if test2==1 & pcdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013!=.
replace cdim12 = dim122020-dim122013 if test2==1 & cdim12==. & dim122022==. & dim122021==. & dim122020!=. & dim122015==. & dim122013!=.
replace pcdim12 = (dim122020-dim122013)/dim122013*100 if test2==1 & pcdim12==. & dim122022==. & dim122021==. & dim122020!=. & dim122015==. & dim122013!=.
replace cdim12 = dim122019-dim122013 if test2==1 & cdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013!=.
replace pcdim12 = (dim122019-dim122013)/dim122013*100 if test2==1 & pcdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013!=.

replace threshold12 = sd122016*dim122016/100 if test2==1 & cdim12==. & dim122015==. & dim122013==. & dim122016!=.
replace cdim12 = dim122019-dim122016 if test2==1 & cdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013==. & dim122016!=.
replace pcdim12 = (dim122019-dim122016)/dim122016*100 if test2==1 & pcdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013==. & dim122016!=.

replace threshold12 = sd122017*dim122017/100 if test2==1 & cdim12==. & dim122015==. & dim122013==. & dim122016==. & dim122017!=.
replace cdim12 = dim122021-dim122017 if test2==1 & cdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013==. & dim122016==. & dim122017!=.
replace pcdim12 = (dim122021-dim122017)/dim122017*100 if test2==1 & pcdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013==. & dim122016==. & dim122017!=.
replace cdim12 = dim122019-dim122017 if test2==1 & cdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013==. & dim122016==. & dim122017!=.
replace pcdim12 = (dim122019-dim122017)/dim122017*100 if test2==1 & pcdim12==. & dim122022==. & dim122021==. & dim122020==. & dim122019!=. & dim122015==. & dim122013==. & dim122016==. & dim122017!=.

replace threshold12 = sd122019*dim122019/100 if test2==1 & cdim12==. & dim122015==. & dim122013==. & dim122016==. & dim122017==. & dim122019!=.
replace cdim12 = dim122021-dim122019 if test2==1 & cdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013==. & dim122016==. & dim122017==. & dim122019!=.
replace pcdim12 = (dim122021-dim122019)/dim122019*100 if test2==1 & pcdim12==. & dim122022==. & dim122021!=. & dim122015==. & dim122013==. & dim122016==. & dim122017==. & dim122019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I12 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim12_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim12_th = 0 if (dim12_th==.) & (test != 1 )
replace dim12_th = . if cdim==.

gen dim12_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim12_50 = 0 if (dim12_50==.) & (test != 1)
replace dim12_50 = . if pcdim==.

gen dim12_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim12_25 = 0 if (dim12_25==.) & (test != 1)
replace dim12_25 = . if pcdim==.

gen dim12_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim12_10 = 0 if (dim12_10==.) & (test != 1)
replace dim12_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim12_*
tab I
tab dim12_th
tab dim12_50
tab dim12_25
tab dim12_10

keep country cd pc I thres dim12_*
save dim12.dta, replace


*#Dimension 13th
clear
use mipac_dims
keep country year dim13 sd13
reshape wide dim13 sd13, i(country) j(year)
gen cdim13 = dim132022-dim132013
gen pcdim13 = (dim132022-dim132013)/dim132013*100
gen threshold13 = sd132013*dim132013/100
*Test1 = Test if there is not info at any year
gen test = 1 if (cdim13==. & dim132013==. & dim132014==. & dim132015==. & dim132016==. & dim132017==. & dim132018==. & dim132019==. & dim132020==. & dim132021==. & dim132022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim13==. & test == .)
*Special treatments
browse country dim* cdim13 if test2==1

replace cdim13 = dim132019-dim132013 if test2==1 & cdim13==. & dim132022==. & dim132021==. & dim132020==. & dim132019!=. & dim132013!=.
replace pcdim13 = (dim132019-dim132013)/dim132013*100 if test2==1 & pcdim13==. & dim132022==. & dim132021==. & dim132020==. & dim132019!=. & dim132013!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I13 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim13_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim13_th = 0 if (dim13_th==.) & (test != 1 )
replace dim13_th = . if cdim==.

gen dim13_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim13_50 = 0 if (dim13_50==.) & (test != 1)
replace dim13_50 = . if pcdim==.

gen dim13_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim13_25 = 0 if (dim13_25==.) & (test != 1)
replace dim13_25 = . if pcdim==.

gen dim13_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim13_10 = 0 if (dim13_10==.) & (test != 1)
replace dim13_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim13_*
tab I
tab dim13_th
tab dim13_50
tab dim13_25
tab dim13_10

keep country cd pc I thres dim13_*
save dim13.dta, replace


*#Dimension 14th
clear
use mipac_dims
keep country
duplicates drop

gen cdim14 = .
gen pcdim14 = .
gen I14 = .
gen threshold14 = .
gen dim14_th=.
gen dim14_50 = .
gen dim14_25 = .
gen dim14_10 = .

browse country cd pc I thres dim14_*
save dim14.dta, replace


*#Dimension 15th
clear
use mipac_dims
keep country year dim15 sd15
reshape wide dim15 sd15, i(country) j(year)
gen cdim15 = dim152022-dim152015
gen pcdim15 = (dim152022-dim152015)/dim152015*100
gen threshold15 = sd152015*dim152015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim15==. & dim152013==. & dim152014==. & dim152015==. & dim152016==. & dim152017==. & dim152018==. & dim152019==. & dim152020==. & dim152021==. & dim152022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim15==. & test == .)

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I15 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim15_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim15_th = 0 if (dim15_th==.) & (test != 1 )
replace dim15_th = . if cdim==.

gen dim15_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim15_50 = 0 if (dim15_50==.) & (test != 1)
replace dim15_50 = . if pcdim==.

gen dim15_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim15_25 = 0 if (dim15_25==.) & (test != 1)
replace dim15_25 = . if pcdim==.

gen dim15_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim15_10 = 0 if (dim15_10==.) & (test != 1)
replace dim15_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim15_*
tab I
tab dim15_th
tab dim15_50
tab dim15_25
tab dim15_10

keep country cd pc I thres dim15_*
save dim15.dta, replace


*#Dimension 16th
clear
use mipac_dims
keep country year dim16 sd16
reshape wide dim16 sd16, i(country) j(year)
gen cdim16 = dim162022-dim162015
gen pcdim16 = (dim162022-dim162015)/dim162015*100
gen threshold16 = sd162016*dim162015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim16==. & dim162013==. & dim162014==. & dim162015==. & dim162016==. & dim162017==. & dim162018==. & dim162019==. & dim162020==. & dim162021==. & dim162022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim16==. & test == .)
*Special treatments
browse country dim* cdim16 if test2==1

replace cdim16 = dim162021-dim162015 if test2==1 & cdim16==. & dim162022==. & dim162021!=. & dim162015!=.
replace pcdim16 = (dim162021-dim162015)/dim162015*100 if test2==1 & pcdim16==. & dim162022==. & dim162021!=. & dim162015!=.
replace cdim16 = dim162020-dim162015 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015!=.
replace pcdim16 = (dim162020-dim162015)/dim162015*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015!=.
replace cdim16 = dim162019-dim162015 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015!=.
replace pcdim16 = (dim162019-dim162015)/dim162015*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015!=.

replace threshold16 = sd162013*dim162013/100 if test2==1 & cdim16==. & dim162015==. & dim162013!=. 
replace cdim16 = dim162022-dim162013 if test2==1 & cdim16==. & dim162022!=. & dim162015==. & dim162013!=.
replace pcdim16 = (dim162022-dim162013)/dim162013*100 if test2==1 & pcdim16==. & dim162022!=. & dim162015==. & dim162013!=.
replace cdim16 = dim162021-dim162013 if test2==1 & cdim16==. & dim162022==. & dim162021!=. & dim162015==. & dim162013!=.
replace pcdim16 = (dim162021-dim162013)/dim162013*100 if test2==1 & pcdim16==. & dim162022==. & dim162021!=. & dim162015==. & dim162013!=.
replace cdim16 = dim162020-dim162013 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013!=.
replace pcdim16 = (dim162020-dim162013)/dim162013*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013!=.
replace cdim16 = dim162019-dim162013 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013!=.
replace pcdim16 = (dim162019-dim162013)/dim162013*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013!=.

replace threshold16 = sd162016*dim162016/100 if test2==1 & cdim16==. & dim162015==. & dim162013==. & dim162016!=.
replace cdim16 = dim162020-dim162016 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016!=.
replace pcdim16 = (dim162020-dim162016)/dim162016*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016!=.
replace cdim16 = dim162019-dim162016 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013==. & dim162016!=.
replace pcdim16 = (dim162019-dim162016)/dim162016*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013==. & dim162016!=.

replace threshold16 = sd162017*dim162017/100 if test2==1 & cdim16==. & dim162015==. & dim162013==. & dim162016==. & dim162017!=.
replace cdim16 = dim162020-dim162017 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016==. & dim162017!=.
replace pcdim16 = (dim162020-dim162017)/dim162017*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016==. & dim162017!=.
replace cdim16 = dim162019-dim162017 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013==. & dim162016==. & dim162017!=.
replace pcdim16 = (dim162019-dim162017)/dim162017*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020==. & dim162019!=. & dim162015==. & dim162013==. & dim162016==. & dim162017!=.

replace threshold16 = sd162019*dim162019/100 if test2==1 & cdim16==. & dim162015==. & dim162013==. & dim162016==. & dim162017==. & dim162019!=.
replace cdim16 = dim162020-dim162019 if test2==1 & cdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016==. & dim162017==. & dim162019!=.
replace pcdim16 = (dim162020-dim162019)/dim162019*100 if test2==1 & pcdim16==. & dim162022==. & dim162021==. & dim162020!=. & dim162015==. & dim162013==. & dim162016==. & dim162017==. & dim162019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I16 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim16_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim16_th = 0 if (dim16_th==.) & (test != 1 )
replace dim16_th = . if cdim==.

gen dim16_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim16_50 = 0 if (dim16_50==.) & (test != 1)
replace dim16_50 = . if pcdim==.

gen dim16_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim16_25 = 0 if (dim16_25==.) & (test != 1)
replace dim16_25 = . if pcdim==.

gen dim16_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim16_10 = 0 if (dim16_10==.) & (test != 1)
replace dim16_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim16_*
tab I
tab dim16_th
tab dim16_50
tab dim16_25
tab dim16_10

keep country cd pc I thres dim16_*
save dim16.dta, replace


*#Dimension 17th
clear
use mipac_dims
keep country year dim17 sd17
reshape wide dim17 sd17, i(country) j(year)
gen cdim17 = dim172022-dim172013
gen pcdim17 = (dim172022-dim172013)/dim172013*100
gen threshold17 = sd172013*dim172013/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim17==. & dim172013==. & dim172014==. & dim172015==. & dim172016==. & dim172017==. & dim172018==. & dim172019==. & dim172020==. & dim172021==. & dim172022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim17==. & test == .)
*Special treatments
browse country dim* cdim17 if test2==1

replace cdim17 = dim172019-dim172013 if	country == "United Kingdom"
replace pcdim17 = (dim172019-dim172013)/dim172013*100 if country == "United Kingdom"

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I17 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim17_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim17_th = 0 if (dim17_th==.) & (test != 1 )
replace dim17_th = . if cdim==.

gen dim17_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim17_50 = 0 if (dim17_50==.) & (test != 1)
replace dim17_50 = . if pcdim==.

gen dim17_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim17_25 = 0 if (dim17_25==.) & (test != 1)
replace dim17_25 = . if pcdim==.

gen dim17_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim17_10 = 0 if (dim17_10==.) & (test != 1)
replace dim17_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim17_*
tab I
tab dim17_th
tab dim17_50
tab dim17_25
tab dim17_10

keep country cd pc I thres dim17_*
save dim17.dta, replace


*#Dimension 18th
clear
use mipac_dims
keep country year dim18 sd18
reshape wide dim18 sd18, i(country) j(year)
gen cdim18 = dim182022-dim182013
gen pcdim18 = (dim182022-dim182013)/dim182013*100
gen threshold18 = sd182013*dim182013/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim18==. & dim182013==. & dim182014==. & dim182015==. & dim182016==. & dim182017==. & dim182018==. & dim182019==. & dim182020==. & dim182021==. & dim182022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim18==. & test == .)
*Special treatments
browse country dim* cdim18 if test2==1

replace cdim18 = dim182021-dim182013 if test2==1 & cdim18==. & dim182022==. & dim182021!=. & dim182013!=.
replace pcdim18 = (dim182021-dim182013)/dim182013*100 if test2==1 & pcdim18==. & dim182022==. & dim182021!=. & dim182013!=.
replace cdim18 = dim182020-dim182013 if test2==1 & cdim18==. & dim182022==. & dim182021==. & dim182020!=. & dim182013!=.
replace pcdim18 = (dim182020-dim182013)/dim182013*100 if test2==1 & pcdim18==. & dim182022==. & dim182021==. & dim182020!=. & dim182013!=.
replace cdim18 = dim182019-dim182013 if test2==1 & cdim18==. & dim182022==. & dim182021==. & dim182020==. & dim182019!=. & dim182013!=.
replace pcdim18 = (dim182019-dim182013)/dim182013*100 if test2==1 & pcdim18==. & dim182022==. & dim182021==. & dim182020==. & dim182019!=. & dim182013!=.

replace threshold18 = sd182019*dim182019/100 if test2==1 & cdim18==. & dim182013==. & dim182016==. & dim182017==. & dim182019!=.
replace cdim18 = dim182020-dim182019 if test2==1 & cdim18==. & dim182022==. & dim182021==. & dim182020!=. & dim182013==. & dim182016==. & dim182017==. & dim182019!=.
replace pcdim18 = (dim182020-dim182019)/dim182019*100 if test2==1 & pcdim18==. & dim182022==. & dim182021==. & dim182020!=. & dim182013==. & dim182016==. & dim182017==. & dim182019!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I18 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim18_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim18_th = 0 if (dim18_th==.) & (test != 1 )
replace dim18_th = . if cdim==.

gen dim18_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim18_50 = 0 if (dim18_50==.) & (test != 1)
replace dim18_50 = . if pcdim==.

gen dim18_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim18_25 = 0 if (dim18_25==.) & (test != 1)
replace dim18_25 = . if pcdim==.

gen dim18_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim18_10 = 0 if (dim18_10==.) & (test != 1)
replace dim18_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim18_*
tab I
tab dim18_th
tab dim18_50
tab dim18_25
tab dim18_10

keep country cd pc I thres dim18_*
save dim18.dta, replace


*#Dimension 19th
clear
use mipac_dims
keep country year dim19 sd19
reshape wide dim19 sd19, i(country) j(year)
gen cdim19 = dim192022-dim192015
gen pcdim19 = (dim192022-dim192015)/dim192015*100
gen threshold19 = sd192015*dim192015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim19==. & dim192013==. & dim192014==. & dim192015==. & dim192016==. & dim192017==. & dim192018==. & dim192019==. & dim192020==. & dim192021==. & dim192022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim19==. & test == .)
*Special treatments
browse country dim* cdim19 if test2==1

replace cdim19 = dim192020-dim192015 if test2==1 & cdim19==. & dim192022==. & dim192021==. & dim192020!=. & dim192015!=.
replace pcdim19 = (dim192020-dim192015)/dim192015*100 if test2==1 & pcdim19==. & dim192022==. & dim192021==. & dim192020!=. & dim192015!=.
replace cdim19 = dim192019-dim192015 if test2==1 & cdim19==. & dim192022==. & dim192021==. & dim192020==. & dim192019!=. & dim192015!=.
replace pcdim19 = (dim192019-dim192015)/dim192015*100 if test2==1 & pcdim19==. & dim192022==. & dim192021==. & dim192020==. & dim192019!=. & dim192015!=.

replace threshold19 = sd192013*dim192013/100 if test2==1 & cdim19==. & dim192015==. & dim192013!=. 
replace cdim19 = dim192019-dim192013 if test2==1 & cdim19==. & dim192022==. & dim192021==. & dim192020==. & dim192019!=. & dim192015==. & dim192014==. & dim192013!=.
replace pcdim19 = (dim192019-dim192013)/dim192013*100 if test2==1 & pcdim19==. & dim192022==. & dim192021==. & dim192020==. & dim192019!=. & dim192015==. & dim192014==. & dim192013!=.

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I19 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim19_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim19_th = 0 if (dim19_th==.) & (test != 1 )
replace dim19_th = . if cdim==.

gen dim19_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim19_50 = 0 if (dim19_50==.) & (test != 1)
replace dim19_50 = . if pcdim==.

gen dim19_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim19_25 = 0 if (dim19_25==.) & (test != 1)
replace dim19_25 = . if pcdim==.

gen dim19_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim19_10 = 0 if (dim19_10==.) & (test != 1)
replace dim19_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim19_*
tab I
tab dim19_th
tab dim19_50
tab dim19_25
tab dim19_10

keep country cd pc I thres dim19_*
save dim19.dta, replace



*#Dimension 20th
clear
use mipac_dims
keep country year dim20 sd20
reshape wide dim20 sd20, i(country) j(year)
gen cdim20 = dim202022-dim202015
gen pcdim20 = (dim202022-dim202015)/dim202015*100
gen threshold20 = sd202015*dim202015/100

*Test1 = Test if there is not info at any year
gen test = 1 if (cdim20==. & dim202013==. & dim202014==. & dim202015==. & dim202016==. & dim202017==. & dim202018==. & dim202019==. & dim202020==. & dim202021==. & dim202022==.)
*Test2 =  Test if there is info at some year and special treatment
gen test2 = 1 if (cdim20==. & test == .)
*Special treatments
browse country dim* cdim20 if test2==1

replace cdim20 = dim202019-dim202015 if	country == "United Kingdom"
replace pcdim20 = (dim202019-dim202015)/dim202015*100 if country == "United Kingdom"

replace pcdim =1000 if pcdim == . & cdim>0  & cdim!=.
gen I20 = 1 if cdim<0
replace I = 0 if cdim>=0
replace I =. if cdim==.

gen dim20_th = 1 if (I==1) & ((cdim*(-1))>=threshold) & (test != 1)
replace dim20_th = 0 if (dim20_th==.) & (test != 1 )
replace dim20_th = . if cdim==.

gen dim20_50 = 1 if (I==1) & ((pcdim*(-1))>=50) & (test != 1)
replace dim20_50 = 0 if (dim20_50==.) & (test != 1)
replace dim20_50 = . if pcdim==.

gen dim20_25 = 1 if (I==1) & ((pcdim*(-1))>=25) & (test != 1)
replace dim20_25 = 0 if (dim20_25==.) & (test != 1)
replace dim20_25 = . if pcdim==.

gen dim20_10 = 1 if (I==1) & ((pcdim*(-1))>=10) & (test != 1)
replace dim20_10 = 0 if (dim20_10==.) & (test != 1)
replace dim20_10 = . if pcdim==.

replace thres=. if cdim==.
browse country cd pc I thres dim20_*
tab I
tab dim20_th
tab dim20_50
tab dim20_25
tab dim20_10

keep country cd pc I thres dim20_*
save dim20.dta, replace


*#Dimension 21th
clear
use mipac_dims
keep country
duplicates drop

gen cdim21 = .
gen pcdim21 = .
gen I21 = .
gen threshold21 = .
gen dim21_th=.
gen dim21_50 = .
gen dim21_25 = .
gen dim21_10 = .

browse country cd pc I thres dim21_*
save dim21.dta, replace


*##Graphs by country
clear
use dim1
merge 1:1 country using dim2
drop _merge
merge 1:1 country using dim3
drop _merge
merge 1:1 country using dim4
drop _merge
merge 1:1 country using dim5
drop _merge
merge 1:1 country using dim6
drop _merge
merge 1:1 country using dim7
drop _merge
merge 1:1 country using dim8
drop _merge
merge 1:1 country using dim9
drop _merge
merge 1:1 country using dim10
drop _merge
merge 1:1 country using dim11
drop _merge
merge 1:1 country using dim12
drop _merge
merge 1:1 country using dim13
drop _merge
merge 1:1 country using dim14
drop _merge
merge 1:1 country using dim15
drop _merge
merge 1:1 country using dim16
drop _merge
merge 1:1 country using dim17
drop _merge
merge 1:1 country using dim18
drop _merge
merge 1:1 country using dim19
drop _merge
merge 1:1 country using dim20
drop _merge
merge 1:1 country using dim21
drop _merge

save MIPAC_dimensions.dta, replace

clear
use MIPAC_dimensions.dta
egen t_dim= rownonmiss(I*)
egen improve = rowtotal(I*) if t_dim>0
egen mipac = rowtotal(*_th) if t_dim>0
egen mipac50 = rowtotal(*_50) if t_dim>0
egen mipac25 = rowtotal(*_25) if t_dim>0
egen mipac10 = rowtotal(*_10) if t_dim>0

gen improv_per = improve/t_dim*100 if improve!=.
gen mipac_per = mipac/t_dim*100 if improve!=.
replace mipac_per =0  if improve==0

tab t_dim
tab improve
tab mipac

*Table by countries
sort country
list country t_dim improve mipac* if t_dim>0

list country t_dim improve if t_dim>0 & improve==0

tab mipac improve if improve>0, cell

gsort -mipac -improve -t_dim country
list country t_dim improve improv_per mipac if improve!=0 & mipac==0

gsort -mipac -improve -t_dim country
list country mipac if t_dim>0

gsort -mipac_per -mipac -improve -t_dim country
list country t_dim improve mipac mipac_per if t_dim>0

tab improv_per
tabstat improv_per, format(%9.2fc)  statistics(q min max)

tab mipac_per
tabstat mipac_per, format(%9.2fc)  statistics(q min max)



collapse (count) *_th

clear
use MIPAC_dimensions.dta
collapse (sum) I*
list

clear
use MIPAC_dimensions.dta
collapse (sum) *_th
list

clear



